#!/bin/sh
#(c) Copyright 2012 Barry Kauler
#2012 GPL licence v3 (refer: /usr/share/doc/legal)
#120629 raspberry pi does not have a hw clock, set date to reasonable value.
#130513 modify handling of "ask" passed param.

. /etc/rc.d/PUPSTATE
. /etc/DISTRO_SPECS #has DISTRO_XORG_AUTO
. /etc/clock
[ "$DISTRO_BINARY_COMPAT" = 'slackware64' ] && lsuffix=64 || lsuffix=

ASKKMAP="no"
ASKLANG="no"
ASKCLOCK="no"

if [ -f /tmp/bootcnt.txt ] ; then #file created in 'init' if pfix=nox.
	ASKKMAP="yes"
	ASKLANG="yes"
fi

#pass a param to ask for keymap and language
if [ $1 ];then
 ASKKMAP="yes"
 ASKLANG="yes"
fi

######keyboard section######
if [ "$ASKKMAP" = "no" ];then
 keymap-set --startup --load &>/dev/null
else
 [ ! -e /etc/keymap ] && keymap-set cli > /dev/console #/usr/sbin/keymap-set
fi
if [ -s /etc/codepage ];then
 CODEPAGE="`cat /etc/codepage`"
 case $CODEPAGE in
  850) modprobe nls_cp850 ;;
  852) modprobe nls_cp852 ; modprobe nls_iso8859-2 ;;
 esac
fi
######end keyboard section######

######locale section######
LANG="`grep '^LANG=' /etc/profile | cut -f 2 -d '='`"
CLANG="`echo "$LANG" | sed -e 's/\.UTF-8/\.utf8/'`" #fix, locale -a always prints .utf8

#precaution - errors in slacko if this dir is missing
mkdir -p /usr/lib${lsuffix}/locale

if [ "$ASKLANG" = "no" ];then
 #'quicksetup' is run 1st time X starts.
 #need this default locale for X to start first time...
 [ ! -d /usr/lib${lsuffix}/locale/en_US ] && localedef -f ISO-8859-1 -i en_US --no-archive en_US >/dev/null
 [ ! -d /usr/lib${lsuffix}/locale/${CLANG} ] && /usr/sbin/chooselocale cli quiet #ask no questions, will run localedef.
else
 #localedef creates locale files in /usr/lib/locale...
 if [ "`locale -a | grep "$CLANG"`" = "" ];then
  if [ "$LANG" = "en_US" ];then
   /usr/sbin/chooselocale cli > /dev/console
  else
   /usr/sbin/chooselocale cli quiet #ask no questions, will run localedef.
  fi
  /sbin/load_consolefont #note, also called from rc.sysinit, xwin.
  ASKCLOCK="yes"
 fi
fi
######end locale section######

######clock section######
#120629 raspberry pi does not have a hw clock, set date to reasonable value...
HWDATE="`hwclock --show 2>/dev/null`" #ex: "Fri 29 Jun 2012 07:45:28 AM WST  -0.725833 seconds"
if [ "$HWDATE" = "" ];then
 if [ -f /var/local/shutdown_date_saved ];then #see /etc/rc.d/rc.shutdown
  date -s "`cat /var/local/shutdown_date_saved`"
 else
  #either of these formats can set the date: "29 JUN 2012 10:00:00" "2012-06-28 16:20:08"
  date -s "`stat --format=%z /etc/DISTRO_SPECS | cut -f 1 -d '.' | cut -f 1,2 -d ' '`" #creation date of build. ex: "2012-06-28 16:20:08"
 fi
else
 if [ "$ASKCLOCK" = "yes" ];then
  /usr/sbin/timezone-set cli > /dev/console
  /usr/sbin/set_hwclock_type cli --hctosys > /dev/console #hw to sys
 else
  /usr/sbin/set_hwclock_type -q --hctosys > /dev/console #hw to sys
 fi
fi
######end clock section######

exit $STATUS
###END###
